package common

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	"log"
	"yangbo/model"
)

var DB *gorm.DB

//数据库连接
func InitDB() *gorm.DB{
	pqInfo := fmt.Sprintf("host=localhost port=5432 user=postgres password=6740Yb dbname=postgres sslmode=disable")
	db,err := gorm.Open("postgres",pqInfo)
	if err != nil {
		panic(err)
	} else {
		log.Println("连接成功！")
	}
	db.AutoMigrate(&model.User{})//自动创建数据表User
	db.AutoMigrate(&model.Introduction{})//创建数据表Introduction
	DB = db
	return  db
}

func GetDB() *gorm.DB{
	return DB
}